<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="React基础篇React 学习 是什么 能干什么 视频+文档+项目 React 教程&#x2F;&#x2F;React Native概念 React是一个用于构建用户界面的JAVASCRIPT库； 构建UI,很多人认为 React 是 MVC 中的 V（视图）； 2013年5月开源，起源于Facebook；  额外：">
<meta property="og:type" content="article">
<meta property="og:title" content="前端&#x2F;Vue+React+Angular&#x2F;React&#x2F;React">
<meta property="og:url" content="http://yoursite.com/2020/02/28/%E5%89%8D%E7%AB%AF/Vue+React+Angular/React/React/index.html">
<meta property="og:site_name" content="heweiliang blog">
<meta property="og:description" content="React基础篇React 学习 是什么 能干什么 视频+文档+项目 React 教程&#x2F;&#x2F;React Native概念 React是一个用于构建用户界面的JAVASCRIPT库； 构建UI,很多人认为 React 是 MVC 中的 V（视图）； 2013年5月开源，起源于Facebook；  额外：">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-02-28T10:11:37.013Z">
<meta property="article:modified_time" content="2020-06-26T07:26:18.482Z">
<meta property="article:author" content="heweiliang">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/02/28/%E5%89%8D%E7%AB%AF/Vue+React+Angular/React/React/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端/Vue+React+Angular/React/React | heweiliang blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="heweiliang blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">heweiliang blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-schedule">

    <a href="/schedule/" rel="section"><i class="fa fa-fw fa-calendar"></i>日程表</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/yourname" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/02/28/%E5%89%8D%E7%AB%AF/Vue+React+Angular/React/React/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="heweiliang">
      <meta itemprop="description" content="heweiliang blog">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="heweiliang blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          前端/Vue+React+Angular/React/React<a href="https://github.com/user-name/repo-name/tree/branch-name/subdirectory-name_posts/%E5%89%8D%E7%AB%AF/Vue+React+Angular/React/React.md" class="post-edit-link" title="编辑" rel="noopener" target="_blank"><i class="fa fa-pencil"></i></a>
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-02-28 18:11:37" itemprop="dateCreated datePublished" datetime="2020-02-28T18:11:37+08:00">2020-02-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-06-26 15:26:18" itemprop="dateModified" datetime="2020-06-26T15:26:18+08:00">2020-06-26</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>18k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>16 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="React基础篇"><a href="#React基础篇" class="headerlink" title="React基础篇"></a>React基础篇</h2><p>React 学习</p>
<p>是什么</p>
<p>能干什么</p>
<p>视频+文档+项目</p>
<h4 id="React-教程-React-Native"><a href="#React-教程-React-Native" class="headerlink" title="React 教程//React Native"></a>React 教程//React Native</h4><h6 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h6><ul>
<li>React是一个用于<strong>构建用户界面的JAVASCRIPT库</strong>；</li>
<li><strong>构建UI</strong>,很多人认为 React 是 MVC 中的 V（视图）；</li>
<li>2013年5月开源，起源于Facebook；</li>
</ul>
<p>额外：</p>
<p>MVC、MVVC、MVP的区别</p>
<p><a href="https://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html" target="_blank" rel="noopener">https://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html</a></p>
<p>MVC （Model-View-Controller）是<strong>最常见的软件架构</strong>之一，业界有着广泛应用</p>
<ul>
<li>视图（View）：用户界面。</li>
<li>控制器（Controller）：业务逻辑</li>
<li>模型（Model）：数据保存</li>
</ul>
<p>MVVC</p>
<p>特点<strong>(声明式设计+高效灵活+JSX+组件+单向响应的数据流)</strong></p>
<ul>
<li><strong>1.声明式设计</strong> −React采用<strong>声明范式</strong>，可以轻松描述应用。</li>
<li><strong>2.高效</strong> −React通过对DOM的模拟，最大限度地减少与DOM的交互。</li>
<li><strong>3.灵活</strong> −React可以与已知的库或框架很好地配合。</li>
<li><strong>4.JSX</strong> − <strong>JSX 是 JavaScript 语法的扩展</strong>。React 开发不一定使用 JSX ，但我们建议使用它。</li>
<li><strong>5.组件</strong> − 通过 React 构建组件，使得代码更加容易得到复用，能够很好的应用在大项目的开发中。</li>
<li><strong>6.单向响应的数据流</strong> − React 实现了<strong>单向响应的数据流，从而减少了重复代码</strong>，这也是它为什么比传统数据绑定更简单。</li>
</ul>
<h6 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang&#x3D;&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;</span><br><span class="line">    &lt;meta name&#x3D;&quot;viewport&quot; content&#x3D;&quot;width&#x3D;device-width, initial-scale&#x3D;1.0&quot;&gt;</span><br><span class="line">    &lt;title&gt;Document&lt;&#x2F;title&gt;</span><br><span class="line">     &#x2F;&#x2F; React + ReactDOM + babel 三个CDN</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react&#x2F;16.4.0&#x2F;umd&#x2F;react.development.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react-dom&#x2F;16.4.0&#x2F;umd&#x2F;react-dom.development.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">    &lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;babel-standalone&#x2F;6.26.0&#x2F;babel.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;div id&#x3D;&quot;example&quot;&gt;&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;script type&#x3D;&quot;text&#x2F;babel&quot;&gt;    </span><br><span class="line"> &#x2F;&#x2F;我们需要使用 JSX，则 &lt;script&gt; 标签的 type 属性需要设置为 text&#x2F;babel。</span><br><span class="line">        ReactDOM.render(&lt;h1&gt;Hello React!!!&lt;&#x2F;h1&gt;, document.getElementById(&quot;example&quot;));</span><br><span class="line">    &lt;&#x2F;script&gt;</span><br><span class="line">&lt;&#x2F;body&gt;</span><br><span class="line">&lt;&#x2F;html&gt;</span><br></pre></td></tr></table></figure>



<h4 id="React-安装"><a href="#React-安装" class="headerlink" title="React 安装"></a>React 安装</h4><h5 id="两种安装方法"><a href="#两种安装方法" class="headerlink" title="两种安装方法"></a>两种安装方法</h5><h6 id="CDN"><a href="#CDN" class="headerlink" title="CDN"></a>CDN</h6><p>引入三个库：  <strong>react.min + rect-dom.min.js + babel.min.js</strong></p>
<ul>
<li>react.min.js ： React 的<strong>核心库</strong></li>
<li>react-dom.min.js :提供与 <strong>DOM 相关</strong>的功能</li>
<li>babel.min.js：将 <strong>ES6 代码转为 ES5 代码</strong>,解决ES6浏览器上执行React 代码</li>
</ul>
<p>Staticfile React CDN </p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 生产环境中不建议使用 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>官方 React CDN</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://unpkg.com/react@16/umd/react.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://unpkg.com/react-dom@16/umd/react-dom.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 生产环境中不建议使用 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://unpkg.com/babel-standalone@6.15.0/babel.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h6 id="通过-npm-使用-React-4步"><a href="#通过-npm-使用-React-4步" class="headerlink" title="通过 npm 使用 React(4步)"></a>通过 npm 使用 React(4步)</h6><ol>
<li><p>安装Node.js(下载，下一步)</p>
</li>
<li><p>国内使用 npm 速度很慢，你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">//更换</span><br><span class="line">npm install -g cnpm --registry=https://registry.npm.taobao.org</span><br><span class="line">npm config <span class="built_in">set</span> registry https://registry.npm.taobao.org</span><br><span class="line">//检测</span><br><span class="line">npm config get registry</span><br><span class="line">或 npm info express</span><br><span class="line">或 cnpm -v</span><br></pre></td></tr></table></figure>
</li>
<li><p>安装React</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cnpm install -g create-react-app 或 npm install -g create-react-app</span><br><span class="line">// cnpm/npm 安装 -全局 create-react-app</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用 create-react-app 快速构建 React 开发环境</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">create-react-app my-app //my-app 项目名</span><br><span class="line">// creat-react-app 项目名</span><br><span class="line"><span class="built_in">cd</span> my-app/</span><br><span class="line">npm start /cnpm start</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h6 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h6><h4 id="React-元素渲染"><a href="#React-元素渲染" class="headerlink" title="React 元素渲染"></a>React 元素渲染</h4><p><strong>元素是构成React应用的最小单位</strong>，它用于描述屏幕上输入的内容</p>
<h6 id="将元素渲染到-DOM-中"><a href="#将元素渲染到-DOM-中" class="headerlink" title="将元素渲染到 DOM 中"></a>将元素渲染到 DOM 中</h6><h6 id="更新元素渲染"><a href="#更新元素渲染" class="headerlink" title="更新元素渲染"></a>更新元素渲染</h6><h4 id="React-JSX-像-XML-的-JavaScript-语法扩展"><a href="#React-JSX-像-XML-的-JavaScript-语法扩展" class="headerlink" title="React JSX 像 XML 的 JavaScript 语法扩展"></a>React JSX <strong>像 XML 的 JavaScript 语法扩展</strong></h4><h6 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h6><p>React 使用 <strong>JSX 来替代常规的 JavaScript</strong>。</p>
<p>JSX 是一个看起来很<strong>像 XML 的 JavaScript 语法扩展</strong>。</p>
<p>我们不需要一定使用 JSX，但它有以下优点：</p>
<ul>
<li>JSX 执行<strong>更快</strong>，因为它在编译为 JavaScript 代码后进行了优化。</li>
<li>它是<strong>类型安全</strong>的，在编译过程中就能发现错误。</li>
<li>使用 JSX <strong>编写模板更加简单快速</strong>。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const element &#x3D; &lt;h1&gt;Hello, world!&lt;&#x2F;h1&gt;;</span><br><span class="line">var myDivElement &#x3D; &lt;div className&#x3D;&quot;foo&quot; &#x2F;&gt;;</span><br><span class="line">ReactDOM.render(myDivElement, document.getElementById(&#39;example&#39;));</span><br></pre></td></tr></table></figure>

<p>这种看起来可能有些奇怪的标签语法既不是字符串也不是 HTML。它被称为 JSX， </p>
<p>一种 JavaScript 的语法扩展。 我们推荐在 React 中使用 JSX 来描述用户界面。</p>
<p>JSX 是在 JavaScript 内部实现的。</p>
<p>我们知道<strong>元素是构成 React 应用的最小单位</strong>，JSX 就是用来<strong>声明 React 当中的元素</strong>。</p>
<p>与浏览器的 DOM 元素不同，<strong>React 当中的元素事实上是普通的对象</strong>，React DOM 可以确保 浏览器 DOM 的数据内容与 React 元素保持一致。</p>
<p><strong>要将 React 元素渲染到根 DOM 节点中，我们通过把它们都传递给 ReactDOM.render() 的方法来将其渲染到页面上</strong>：</p>
<p>注意</p>
<p>由于 JSX 就是 JavaScript，一些标识符像 <code>class</code> 和 <code>for</code> 不建议作为 XML 属性名。作为替代，React DOM 使用 <code>className</code> 和 <code>htmlFor</code> 来做对应的属性。</p>
<h6 id="使用JSX"><a href="#使用JSX" class="headerlink" title="使用JSX"></a>使用JSX</h6><ul>
<li>代码中嵌套多个 HTML 标签，需要使用一个 div 元素包裹它</li>
<li>实例中的 p 元素添加了自定义属性 <strong>data-myattribute</strong>，添加自定义属性需要使用 <strong>data-</strong> 前缀。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">    &lt;h1&gt;h1&lt;&#x2F;h1&gt;</span><br><span class="line">    &lt;h2&gt;h2&lt;&#x2F;h2&gt;</span><br><span class="line">    &lt;p data-myattribute &#x3D; &quot;somevalue&quot;&gt;这是一个很不错的 JavaScript 库!&lt;&#x2F;p&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    ,</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h6 id="独立文件"><a href="#独立文件" class="headerlink" title="独立文件"></a>独立文件</h6><p>React JSX代码可以放在一个独立文件(创建文件helloworld_react.js)上</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type&#x3D;&quot;text&#x2F;babel&quot; src&#x3D;&quot;helloworld_react.js&quot;&gt;&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h6 id="Javascript表达式"><a href="#Javascript表达式" class="headerlink" title="Javascript表达式"></a>Javascript表达式</h6><p>在JSX中使用Javasript表达式。表达式写在花括号{}中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;12+90&#125;;</span><br></pre></td></tr></table></figure>

<p>注意：在 JSX 中不能使用 <strong>if else</strong> 语句，但可以使用 <strong>conditional (三元运算)</strong> 表达式来替代</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type&#x3D;&quot;text&#x2F;babel&quot;&gt;</span><br><span class="line">var i &#x3D; 10;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;h1&gt;&#123;i &#x3D;&#x3D; 1 ? &#39;True!&#39; : &#39;False&#39;&#125;&lt;&#x2F;h1&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">    ,</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h6 id="样式"><a href="#样式" class="headerlink" title="样式"></a>样式</h6><p>React 推荐使用内联样式。我们可以使用 <strong>camelCase</strong> 语法来设置内联样式.  <strong>style = {样式名}</strong></p>
<p><strong>React 会在指定元素数字后自动添加 px</strong> 。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">var myStyle &#x3D; &#123;</span><br><span class="line">    fontSize: 100,&#123;&#x2F;*React 会在指定元素数字后自动添加 px  *&#x2F;&#125;</span><br><span class="line">    color: &#39;#FF0000&#39;</span><br><span class="line">&#125;;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;h1 style &#x3D; &#123;myStyle&#125;&gt;菜鸟教程&lt;&#x2F;h1&gt;,</span><br><span class="line">   &#123;&#x2F;*</span><br><span class="line">   style &#x3D; &#123;样式名&#125;</span><br><span class="line">   *&#x2F;&#125;</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h6 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h6><p>注释需要写在花括号中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#x2F;*注释....*&#x2F;&#125;</span><br></pre></td></tr></table></figure>

<h6 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h6><p>JSX 允许在模板中插入数组，数组会自动展开所有成员：</p>
<p>使用</p>
<p>定义 var 数组名 = [元素标签1，元素标签2]；</p>
<p>引用 {数组名}</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var arr &#x3D; [&lt;p&gt;p&lt;&#x2F;p&gt;,&lt;h1&gt;h1&lt;&#x2F;h1&gt;]; &#123;&#x2F;* 数组的定义*&#x2F;&#125;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;div&gt;&#123;arr&#125;&lt;&#x2F;div&gt;,</span><br><span class="line">    doucuement.getElementById(&#39;example&#39;);</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h4 id="React-组件"><a href="#React-组件" class="headerlink" title="React 组件"></a>React 组件</h4><p><strong>组件使得我们的应用更容易来管理</strong></p>
<h6 id="定义组件"><a href="#定义组件" class="headerlink" title="定义组件"></a>定义组件</h6><ul>
<li><p>使用函数定义组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function HelloMessage(props)&#123;</span><br><span class="line">	return &lt;h1&gt;Hello World&lt;&#x2F;h1&gt;;</span><br><span class="line">&#125;</span><br><span class="line">const element &#x3D; &lt;HelloMessage &#x2F;&gt;;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">	element,doucument.getELementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用ES6 class来定义一个组件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">class Wellcome extends React.Component&#123;</span><br><span class="line">	render()&#123;</span><br><span class="line">		return &lt;h1&gt;Hello React!!!&lt;&#x2F;h1&gt;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>参数传递 </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function 方法名(props)&#123;</span><br><span class="line">    return &lt;h1&gt;&lt;&#123;props.name&#125;&#x2F;h1&gt;</span><br><span class="line">&#125;</span><br><span class="line">const element &#x3D; &lt;方法名 name&#x3D;&quot;属性&quot;&gt;</span><br><span class="line">ReactDOM.render(</span><br><span class="line"> 	element,doucument.getElementById(&#39;&#39;);        </span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function HellMessages(props)&#123;</span><br><span class="line">	return &lt;h1&gt;Helle &#123;props.name&#125;&lt;&#x2F;h1&gt;</span><br><span class="line">&#125;</span><br><span class="line">const element &#x3D; &lt;HelloMessage name&#x3D;&quot;Message&quot;&gt;;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">	element,document.getElementById(&#39;example&#39;);</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>注意</p>
<p>原生 HTML 元素名以小写字母开头，而<strong>自定义的 React 类名以大写字母开头</strong>，比如 HelloMessage 不能写成 helloMessage。除此之外还需要注意组件类只能包含一个顶层标签，否则也会报错。</p>
</li>
</ul>
<h6 id="复合组件"><a href="#复合组件" class="headerlink" title="复合组件"></a>复合组件</h6><p>我们可以通过创建多个组件来合成一个组件，即把组件的不同功能点进行分离。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">function Name(props) &#123;</span><br><span class="line">      return &lt;h1&gt;&#123;props.name&#125;&lt;&#x2F;h1&gt;;</span><br><span class="line">&#125;</span><br><span class="line">function Age(props) &#123;</span><br><span class="line">      return &lt;h1&gt;&#123;props.age&#125;&lt;&#x2F;h1&gt;;</span><br><span class="line">&#125;</span><br><span class="line">function Ssex(props) &#123;</span><br><span class="line">      return &lt;h1&gt;&#123;props.sex&#125;&lt;&#x2F;h1&gt;;</span><br><span class="line">&#125;</span><br><span class="line">function App() &#123;</span><br><span class="line"> 	  return (</span><br><span class="line">        &lt;div&gt;</span><br><span class="line">            &lt;Name name&#x3D;&quot;张三&quot; &#x2F;&gt; &#x2F;&#123;&#x2F;*不用符号结尾 *&#x2F;&#125;</span><br><span class="line">            &lt;Age age&#x3D;&quot;18&quot; &#x2F;&gt;</span><br><span class="line">            &lt;Ssex sex&#x3D;&quot;男&quot; &#x2F;&gt;</span><br><span class="line">         &lt;&#x2F;div&gt;</span><br><span class="line">); &#123;&#x2F;* 结尾;号*&#x2F;&#125;</span><br><span class="line">&#125;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">            &lt;App &#x2F;&gt;, document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h4 id="React-State-状态"><a href="#React-State-状态" class="headerlink" title="React State(状态)"></a>React State(状态)</h4><h6 id="State状态的概念-组件渲染用户界面的状态"><a href="#State状态的概念-组件渲染用户界面的状态" class="headerlink" title="State状态的概念 组件渲染用户界面的状态"></a>State状态的概念 组件渲染用户界面的状态</h6><p><strong>React 把组件看成是一个状态机</strong>（State Machines）。</p>
<p><strong>通过与用户的交互，实现不同状态，然后渲染 UI，让用户界面和数据保持一致。</strong></p>
<p>React 里，只需<strong>更新组件的 state</strong>，然后根据<strong>新的 state 重新渲染用户界面（不要操作 DOM）。</strong></p>
<p>以下实例创建一个名称扩展为 React.Component 的 ES6 类，在 render() 方法中使用 this.state 来修改当前的时间。</p>
<p>添加一个类构造函数来初始化状态 this.state，类组件应始终使用 props 调用基础构造函数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">class Clock extends React.Component &#123;</span><br><span class="line">&#x2F;&#x2F;定义一个Clock类进行封装Clock组件    </span><br><span class="line">  constructor(props) &#123; &#x2F;&#x2F;constructor 构造器 一个类构造函数来初始化状态this.state</span><br><span class="line">    super(props);</span><br><span class="line">    this.state &#x3D; &#123;date: new Date()&#125;;</span><br><span class="line">  &#125;</span><br><span class="line">  render() &#123; &#x2F;&#x2F;渲染函数</span><br><span class="line">    return (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &lt;h1&gt;Hello, world!&lt;&#x2F;h1&gt;</span><br><span class="line">        &lt;h2&gt;现在是 &#123;this.state.date.toLocaleTimeString()&#125;.&lt;&#x2F;h2&gt;</span><br><span class="line">      &lt;&#x2F;div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Clock &#x2F;&gt;,</span><br><span class="line">  document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h6 id="将生命周期方法添加到类中-React生命周期）"><a href="#将生命周期方法添加到类中-React生命周期）" class="headerlink" title="将生命周期方法添加到类中 (React生命周期）"></a>将<strong>生命周期</strong>方法添加到类中 (React生命周期）</h6><p>在具有许多组件的应用程序中，在销毁时释放组件所占用的资源非常重要。</p>
<p>每当 Clock 组件第一次<strong>加载到 DOM 中的时候，我们都想生成定时器</strong>，这在 React 中被称为<strong>挂载</strong>。</p>
<p>同样，每当 Clock 生成的这个 <strong>DOM 被移除的时候，我们也会想要清除定时器</strong>，这在 React 中被称为<strong>卸载</strong>。</p>
<p>我们可以在组件类上声明特殊的方法，当组件挂载或卸载时，来运行一些代码：</p>
<table>
<thead>
<tr>
<th><strong>componentDidMount()</strong> 与 <strong>componentWillUnmount()</strong></th>
<th>生命周期钩子</th>
</tr>
</thead>
<tbody><tr>
<td><strong>componentDidMount()</strong> 钩子</td>
<td>设置一个定时器</td>
</tr>
<tr>
<td><strong>componentWillUnmount()</strong>钩子</td>
<td>卸载定时器</td>
</tr>
</tbody></table>
<p><strong>componentDidMount()</strong> 与 <strong>componentWillUnmount()</strong> 方法被称作生命周期钩子。</p>
<p>在组件输出到 DOM 后会执行 <strong>componentDidMount()</strong> 钩子，我们就可以在这个钩子上设置一个定时器。</p>
<p>this.timerID 为定时器的 ID，我们可以在 <strong>componentWillUnmount()</strong> 钩子中卸载定时器。</p>
<p><strong>定时器例子</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">class Clock extends React.Component &#123;</span><br><span class="line">  constructor(props) &#123; &#x2F;&#x2F;构造函数</span><br><span class="line">    super(props);</span><br><span class="line">    this.state &#x3D; &#123;date: new Date()&#125;;</span><br><span class="line">  &#125;</span><br><span class="line"> &#x2F;&#x2F;生命周期钩子 componentDidMount()和componentWillUnmount()</span><br><span class="line">  componentDidMount() &#123;</span><br><span class="line">    this.timerID &#x3D; setInterval(</span><br><span class="line">      () &#x3D;&gt; this.tick(),</span><br><span class="line">      1000</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  componentWillUnmount() &#123;</span><br><span class="line">    clearInterval(this.timerID);</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  tick() &#123;</span><br><span class="line">    this.setState(&#123;</span><br><span class="line">      date: new Date()</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  render() &#123; &#x2F;&#x2F;渲染函数</span><br><span class="line">    return (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &lt;h1&gt;Hello, world!&lt;&#x2F;h1&gt;</span><br><span class="line">        &lt;h2&gt;现在是 &#123;this.state.date.toLocaleTimeString()&#125;.&lt;&#x2F;h2&gt;</span><br><span class="line">      &lt;&#x2F;div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;Clock &#x2F;&gt;,</span><br><span class="line">  document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>





<h6 id="数据自顶向下流动-不知道组件状态的"><a href="#数据自顶向下流动-不知道组件状态的" class="headerlink" title="数据自顶向下流动 不知道组件状态的"></a>数据自顶向下流动 不知道组件状态的</h6><p><strong>父组件或子组件都不能知道某个组件是有状态还是无状态</strong>，并且它们<strong>不应该关心某组件是被定义为一个函数还是一个类。</strong></p>
<p>这就是为什么<strong>状态通常被称为局部或封装</strong>。 除了拥有并设置它的组件外，其它组件不可访问。</p>
<p>以下实例中 FormattedDate 组件将在其属性中接收到 date 值，并且不知道它是来自 Clock 状态、还是来自 Clock 的属性、亦或手工输入：</p>
<h4 id="React-Props"><a href="#React-Props" class="headerlink" title="React Props"></a>React Props</h4><p>state 和 props 主要的区别在于 <strong>props 是不可变的</strong>，而 <strong>state 可以根据与用户交互来改变</strong>。这就是为什么<strong>有些容器组件需要定义 state 来更新和修改数据</strong>。 而子组件只能通过 <strong>props 来传递数据。</strong></p>
<table>
<thead>
<tr>
<th>props（道具）</th>
<th>不可变的</th>
<th>传递数据</th>
</tr>
</thead>
<tbody><tr>
<td>state(状态)</td>
<td>根据与用户交互来改变</td>
<td>更新和修改数据</td>
</tr>
</tbody></table>
<h6 id="使用Props"><a href="#使用Props" class="headerlink" title="使用Props"></a>使用Props</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">function HelloMessage(props) &#123;</span><br><span class="line">    return &lt;h1&gt;Hello &#123;props.name&#125;!&lt;&#x2F;h1&gt;;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F; name属性可以通过props.name来获取 </span><br><span class="line">const element &#x3D; &lt;HelloMessage name&#x3D;&quot;Message&quot;&#x2F;&gt;;</span><br><span class="line"> </span><br><span class="line">ReactDOM.render(</span><br><span class="line">    element,</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<h6 id="默认Props"><a href="#默认Props" class="headerlink" title="默认Props"></a>默认Props</h6><p>组件类的defaultProps属性设置默认值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">class HelloMessage extends React.Component&#123;</span><br><span class="line"> 	render()&#123;</span><br><span class="line"> 		return(</span><br><span class="line"> 			&lt;h1&gt;Hello,&#123;this.props.name&#125;&lt;&#x2F;h1&gt;</span><br><span class="line"> 		);</span><br><span class="line"> 	&#125;</span><br><span class="line">&#125;</span><br><span class="line">HelloMessage.defaultProps &#x3D; &#123;</span><br><span class="line">	name : &#39;Message&#39;</span><br><span class="line">&#125;;</span><br><span class="line">const element &#x3D; &lt;HelloMessage &#x2F;&gt;;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">	element,doucumentById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h6 id="State和Props"><a href="#State和Props" class="headerlink" title="State和Props"></a>State和Props</h6><p>如何在应用中组合使用 state 和 props 。我们可以<strong>在父组件中设置 state</strong>， 并通过<strong>在子组件上使用 props 将其传递到子组件上</strong>。在 render 函数中, 我们设置 name 和 site 来获取父组件传递过来的数据。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 三个组件 WebSite父组件和Name和Link子组件</span><br><span class="line">class WebSite extends React.Component &#123;</span><br><span class="line">  constructor() &#123;</span><br><span class="line">      super();</span><br><span class="line">      this.state &#x3D; &#123;</span><br><span class="line">        name: &quot;Name&quot;,</span><br><span class="line">        site: &quot;Site&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">        &#x2F;&#x2F; 两个子组件的使用</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &lt;Name name&#x3D;&#123;this.state.name&#125; &#x2F;&gt;</span><br><span class="line">        &lt;Link site&#x3D;&#123;this.state.site&#125; &#x2F;&gt;</span><br><span class="line">      &lt;&#x2F;div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class Name extends React.Component &#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">      &lt;h1&gt;&#123;this.props.name&#125;&lt;&#x2F;h1&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">class Link extends React.Component &#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">      &lt;a href&#x3D;&#123;this.props.site&#125;&gt;</span><br><span class="line">        &#123;this.props.site&#125;</span><br><span class="line">      &lt;&#x2F;a&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;WebSite &#x2F;&gt;,</span><br><span class="line">  document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br><span class="line">&#x2F;&#x2F;结果</span><br><span class="line">&#x2F;&#x2F; Name Site(链接的格式)</span><br></pre></td></tr></table></figure>

<h6 id="Props验证（propTypes）"><a href="#Props验证（propTypes）" class="headerlink" title="Props验证（propTypes）"></a>Props验证（propTypes）</h6><p><strong>React.PropTypes 在 React v15.5 版本后已经移到了</strong> <strong>prop-types</strong> <strong>库。</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.bootcss.com&#x2F;prop-types&#x2F;15.6.1&#x2F;prop-types.js&quot;&gt;&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<p>Props 验证使用 <strong>propTypes</strong>，它可以<strong>保证我们的应用组件被正确使用</strong>，</p>
<p>React.PropTypes 提供很多验证器 (validator) 来验证传入数据是否有效。当向 props 传入无效数据时，JavaScript 控制台会抛出警告。</p>
<p>实例:创建一个 Mytitle 组件，属性 title 是必须的且是字符串<strong>，非字符串类型会自动转换为字符串</strong> ：</p>
<p>React 16.4版本</p>
<p>使用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">组件.ropTypes &#x3D; &#123;</span><br><span class="line">	titile:PropTypes.string</span><br><span class="line">	&#x2F;&#x2F;过滤规则</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react&#x2F;16.4.0&#x2F;umd&#x2F;react.development.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react-dom&#x2F;16.4.0&#x2F;umd&#x2F;react-dom.development.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;prop-types&#x2F;15.6.1&#x2F;prop-types.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;babel-standalone&#x2F;6.26.0&#x2F;babel.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">var title &#x3D; &quot;HelloTitle&quot;;</span><br><span class="line">&#x2F;&#x2F; var title &#x3D; 123;</span><br><span class="line">class MyTitle extends React.Component &#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    return (</span><br><span class="line">      &lt;h1&gt;Hello, &#123;this.props.title&#125;&lt;&#x2F;h1&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">MyTitle.propTypes &#x3D; &#123;</span><br><span class="line">  title: PropTypes.string</span><br><span class="line">&#125;;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;MyTitle title&#x3D;&#123;title&#125; &#x2F;&gt;, &#x2F;&#x2F;要传递参数</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>Reacr 15.4</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react&#x2F;15.4.2&#x2F;react.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;react&#x2F;15.4.2&#x2F;react-dom.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line">&lt;script src&#x3D;&quot;https:&#x2F;&#x2F;cdn.staticfile.org&#x2F;babel-standalone&#x2F;6.22.1&#x2F;babel.min.js&quot;&gt;&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line">var title &#x3D; &quot;HelloTitle&quot;;</span><br><span class="line">&#x2F;&#x2F; var title &#x3D; 123;</span><br><span class="line">var MyTitle &#x3D; React.createClass(&#123;</span><br><span class="line">  propTypes: &#123;</span><br><span class="line">    title: React.PropTypes.string.isRequired,</span><br><span class="line">  &#125;,</span><br><span class="line"> </span><br><span class="line">  render: function() &#123;</span><br><span class="line">     return &lt;h1&gt; &#123;this.props.title&#125; &lt;&#x2F;h1&gt;;</span><br><span class="line">   &#125;</span><br><span class="line">&#125;);</span><br><span class="line">ReactDOM.render(</span><br><span class="line">    &lt;MyTitle title&#x3D;&#123;title&#125; &#x2F;&gt;,</span><br><span class="line">    document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h4 id="React-事件处理"><a href="#React-事件处理" class="headerlink" title="React 事件处理"></a>React 事件处理</h4><p>React 元素的事件处理和 DOM 元素类似。但是有一点语法上的不同:</p>
<ul>
<li>React 事件绑定属性的命名采用驼峰式写法，而不是小写。</li>
<li>如果采用 JSX 的语法你需要传入一个函数作为事件处理函数，而不是一个字符串(DOM 元素的写法)</li>
</ul>
<h4 id="React-条件渲染"><a href="#React-条件渲染" class="headerlink" title="React 条件渲染"></a>React 条件渲染</h4><h4 id="React-列表-amp-Keys"><a href="#React-列表-amp-Keys" class="headerlink" title="React 列表 &amp; Keys"></a>React 列表 &amp; Keys</h4><h4 id="React-组件-API"><a href="#React-组件-API" class="headerlink" title="React 组件 API"></a>React 组件 API</h4><h6 id="React-API"><a href="#React-API" class="headerlink" title="React API"></a>React API</h6><table>
<thead>
<tr>
<th>方法名</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>setState</td>
<td>设置状态</td>
</tr>
<tr>
<td>replaceState</td>
<td>设置状态</td>
</tr>
<tr>
<td>setProps</td>
<td>设置属性</td>
</tr>
<tr>
<td>replaceProps</td>
<td>替换属性</td>
</tr>
<tr>
<td>forceUpdate</td>
<td>替换属性</td>
</tr>
<tr>
<td>findDOMNode</td>
<td>获取DOM节点</td>
</tr>
<tr>
<td>findDOMNode</td>
<td>判断组件挂载状态</td>
</tr>
</tbody></table>
<h4 id="React-组件生命周期"><a href="#React-组件生命周期" class="headerlink" title="React 组件生命周期"></a>React 组件生命周期</h4><p>组件的生命周期可分成三个状态：</p>
<ul>
<li>Mounting：已插入真实 DOM</li>
<li>Updating：正在被重新渲染</li>
<li>Unmounting：已移出真实 DOM</li>
</ul>
<h4 id="React-AJAX"><a href="#React-AJAX" class="headerlink" title="React AJAX"></a>React AJAX</h4><p><strong>AJAX 用JavaScript执行异步网络请求</strong></p>
<p><strong>React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取</strong>，</p>
<p><strong>当从服务端获取数据时可以将数据存储在 state 中</strong>，再用 <strong>this.setState 方法重新渲染 UI</strong>。</p>
<p><strong>当使用异步加载数据时，在组件卸载前使用 componentWillUnmount 来取消未完成的请求。</strong></p>
<p>以下实例演示了获取 Github 用户最新 gist 共享描述:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">title</span>&gt;</span>React 实例<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">//使用 jQuery 完成 Ajax 请求    </span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/react/16.4.0/umd/react.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/jquery/2.1.4/jquery.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"example"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/babel"</span>&gt;</span></span><br><span class="line"><span class="actionscript"><span class="class"><span class="keyword">class</span> <span class="title">UserGist</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span></span><br><span class="line"><span class="javascript">  <span class="keyword">constructor</span>(props) &#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">super</span>(props);</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.state = &#123;username: <span class="string">''</span>, lastGistUrl: <span class="string">''</span>&#125;;</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">  componentDidMount() &#123;</span><br><span class="line"><span class="actionscript">    <span class="keyword">this</span>.serverRequest = $.get(<span class="keyword">this</span>.props.source, <span class="function"><span class="keyword">function</span> <span class="params">(result)</span> </span>&#123;</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">var</span> lastGist = result[<span class="number">0</span>];</span></span><br><span class="line"><span class="actionscript">      <span class="keyword">this</span>.setState(&#123;</span></span><br><span class="line">        username: lastGist.owner.login,</span><br><span class="line">        lastGistUrl: lastGist.html_url</span><br><span class="line">      &#125;);</span><br><span class="line"><span class="actionscript">    &#125;.bind(<span class="keyword">this</span>));</span></span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  componentWillUnmount() &#123;</span><br><span class="line"><span class="actionscript">    <span class="keyword">this</span>.serverRequest.abort();</span></span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  render() &#123;</span><br><span class="line"><span class="actionscript">    <span class="keyword">return</span> (</span></span><br><span class="line"><span class="handlebars"><span class="xml">      <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="actionscript">        &#123;<span class="keyword">this</span>.state.username&#125; 用户最新的 Gist 共享地址：</span></span><br><span class="line"><span class="handlebars"><span class="xml">        <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&#123;this.state.lastGistUrl&#125;</span>&gt;</span>&#123;this.state.lastGistUrl&#125;<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span></span></span><br><span class="line"><span class="handlebars"><span class="xml">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">ReactDOM.render(</span><br><span class="line"><span class="handlebars"><span class="xml">  <span class="tag">&lt;<span class="name">UserGist</span> <span class="attr">source</span>=<span class="string">"https://api.github.com/users/octocat/gists"</span> /&gt;</span>,</span></span></span><br><span class="line"><span class="javascript">  <span class="built_in">document</span>.getElementById(<span class="string">'example'</span>)</span></span><br><span class="line">);</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>





<h4 id="React-表单与事件"><a href="#React-表单与事件" class="headerlink" title="React 表单与事件"></a>React 表单与事件</h4><p>React表单</p>
<p>Select下拉菜单</p>
<p>多个表单</p>
<p>React事件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">class HelloMessage extends React.Component &#123;</span><br><span class="line">            constructor(props) &#123;</span><br><span class="line">                super(props);</span><br><span class="line">                this.state &#x3D; &#123; value: &#39;Hello React&#39; &#125;;</span><br><span class="line">                this.handleChange &#x3D; this.handleChange.bind(this);</span><br><span class="line">            &#125;</span><br><span class="line">            handleChange(event) &#123;</span><br><span class="line">                this.setState(&#123; value: &#39;Hello World&#39; &#125;); &#x2F;&#x2F;setState 设置状态</span><br><span class="line">            &#125;</span><br><span class="line">            render() &#123;</span><br><span class="line">                var value &#x3D; this.state.value;</span><br><span class="line">                return (</span><br><span class="line">                    &lt;div&gt;</span><br><span class="line">                        &lt;button onClick&#x3D;&#123;this.handleChange&#125;&gt;按钮&lt;&#x2F;button&gt;</span><br><span class="line">                        &lt;h4&gt;&#123;value&#125;&lt;&#x2F;h4&gt;</span><br><span class="line">                    &lt;&#x2F;div&gt;</span><br><span class="line">                );</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        ReactDOM.render(</span><br><span class="line">            &lt;HelloMessage &#x2F;&gt;, document.getElementById(&#39;example&#39;)</span><br><span class="line">        );</span><br></pre></td></tr></table></figure>

<p>当你需要从子组件中更新父组件的 <strong>state</strong> 时，你需要在父组件通过创建事件句柄 (<strong>handleChange</strong>) ，并作为 prop (<strong>updateStateProp</strong>) 传递到你的子组件上。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">class Content extends React.Component &#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    return  &lt;div&gt;</span><br><span class="line">              &lt;button onClick &#x3D; &#123;this.props.updateStateProp&#125;&gt;点我&lt;&#x2F;button&gt;</span><br><span class="line">              &lt;h4&gt;&#123;this.props.myDataProp&#125;&lt;&#x2F;h4&gt;</span><br><span class="line">           &lt;&#x2F;div&gt;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">class HelloMessage extends React.Component &#123;</span><br><span class="line">  constructor(props) &#123;</span><br><span class="line">      super(props);</span><br><span class="line">      this.state &#x3D; &#123;value: &#39;Hello Runoob!&#39;&#125;;</span><br><span class="line">      this.handleChange &#x3D; this.handleChange.bind(this);</span><br><span class="line">  &#125;</span><br><span class="line">  handleChange(event) &#123;</span><br><span class="line">    this.setState(&#123;value: &#39;菜鸟教程&#39;&#125;)</span><br><span class="line">  &#125;</span><br><span class="line">  render() &#123;</span><br><span class="line">    var value &#x3D; this.state.value;</span><br><span class="line">    return &lt;div&gt;</span><br><span class="line">            &lt;Content myDataProp &#x3D; &#123;value&#125; </span><br><span class="line">              updateStateProp &#x3D; &#123;this.handleChange&#125;&gt;&lt;&#x2F;Content&gt;</span><br><span class="line">        &#x2F;&#x2F;myDataProp &#x3D; &#123;value&#125;  传输参数</span><br><span class="line">     	&#x2F;&#x2F; updateStateProp &#x3D; &#123;this.handleChange&#125; </span><br><span class="line">           &lt;&#x2F;div&gt;;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;HelloMessage &#x2F;&gt;,</span><br><span class="line">  document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h4 id="React-Refs"><a href="#React-Refs" class="headerlink" title="React Refs"></a>React Refs</h4><p><em>refs abbr. references （与to连用）提及, 涉及, 参考 n. 裁判员( ref的复数形式 )</em></p>
<p>React 支持<strong>一种非常特殊的属性 Ref</strong> ，你可以<strong>用来绑定到 render() 输出的任何组件</strong>上。</p>
<p>这个特殊的属性允许你引用 render() 返回的相应的支撑实例（ backing instance ）。这样就可以确保在任何时间总是拿到正确的实例。</p>
<p>使用方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">this.refs.myInput.focus();   &#x2F;&#x2F;this.refs(重点)</span><br><span class="line"> &lt;input type&#x3D;&quot;text&quot; ref&#x3D;&quot;myInput&quot; &#x2F;&gt; &#x2F;&#x2F;绑定render函数的input</span><br></pre></td></tr></table></figure>

<p>绑定一个 ref 属性到 render 的返回值上：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">class MyComponent extends React.Component &#123;</span><br><span class="line">  handleClick() &#123;</span><br><span class="line">    &#x2F;&#x2F; 使用原生的 DOM API 获取焦点</span><br><span class="line">    this.refs.myInput.focus();</span><br><span class="line">  &#125;</span><br><span class="line">  render() &#123;</span><br><span class="line">    &#x2F;&#x2F;  当组件插入到 DOM 后，ref 属性添加一个组件的引用于到 this.refs</span><br><span class="line">    return (</span><br><span class="line">      &lt;div&gt;</span><br><span class="line">        &lt;input type&#x3D;&quot;text&quot; ref&#x3D;&quot;myInput&quot; &#x2F;&gt;</span><br><span class="line">        &lt;input</span><br><span class="line">          type&#x3D;&quot;button&quot;</span><br><span class="line">          value&#x3D;&quot;点我输入框获取焦点&quot;</span><br><span class="line">          onClick&#x3D;&#123;this.handleClick.bind(this)&#125;</span><br><span class="line">        &#x2F;&gt;</span><br><span class="line">      &lt;&#x2F;div&gt;</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line">ReactDOM.render(</span><br><span class="line">  &lt;MyComponent &#x2F;&gt;,</span><br><span class="line">  document.getElementById(&#39;example&#39;)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>事件绑定</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">按钮 onClick&#x3D;&#123;this.函数名.bind(this)&#125;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="heweiliang 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="heweiliang 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/02/27/%E5%89%8D%E7%AB%AF/%E7%BD%91%E9%A1%B5%E3%80%81APP%E3%80%81%E5%B0%8F%E7%A8%8B%E5%BA%8F%E3%80%81PC/WeChatApplet/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" rel="prev" title="前端/网页、APP、小程序、PC/WeChatApplet/微信小程序">
      <i class="fa fa-chevron-left"></i> 前端/网页、APP、小程序、PC/WeChatApplet/微信小程序
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/05/%E5%89%8D%E7%AB%AF/Node.js/nodejs%E8%A7%86%E9%A2%91/" rel="next" title="前端/Node.js/nodejs视频">
      前端/Node.js/nodejs视频 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC81MDY1MC8yNzEzMw=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#React基础篇"><span class="nav-number">1.</span> <span class="nav-text">React基础篇</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#React-教程-React-Native"><span class="nav-number">1.0.1.</span> <span class="nav-text">React 教程&#x2F;&#x2F;React Native</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#概念"><span class="nav-number">1.0.1.0.1.</span> <span class="nav-text">概念</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#实例"><span class="nav-number">1.0.1.0.2.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-安装"><span class="nav-number">1.0.2.</span> <span class="nav-text">React 安装</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#两种安装方法"><span class="nav-number">1.0.2.1.</span> <span class="nav-text">两种安装方法</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#CDN"><span class="nav-number">1.0.2.1.1.</span> <span class="nav-text">CDN</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#通过-npm-使用-React-4步"><span class="nav-number">1.0.2.1.2.</span> <span class="nav-text">通过 npm 使用 React(4步)</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#目录结构"><span class="nav-number">1.0.2.1.3.</span> <span class="nav-text">目录结构</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-元素渲染"><span class="nav-number">1.0.3.</span> <span class="nav-text">React 元素渲染</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#将元素渲染到-DOM-中"><span class="nav-number">1.0.3.0.1.</span> <span class="nav-text">将元素渲染到 DOM 中</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#更新元素渲染"><span class="nav-number">1.0.3.0.2.</span> <span class="nav-text">更新元素渲染</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-JSX-像-XML-的-JavaScript-语法扩展"><span class="nav-number">1.0.4.</span> <span class="nav-text">React JSX 像 XML 的 JavaScript 语法扩展</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#语法"><span class="nav-number">1.0.4.0.1.</span> <span class="nav-text">语法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#使用JSX"><span class="nav-number">1.0.4.0.2.</span> <span class="nav-text">使用JSX</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#独立文件"><span class="nav-number">1.0.4.0.3.</span> <span class="nav-text">独立文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Javascript表达式"><span class="nav-number">1.0.4.0.4.</span> <span class="nav-text">Javascript表达式</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#样式"><span class="nav-number">1.0.4.0.5.</span> <span class="nav-text">样式</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#注释"><span class="nav-number">1.0.4.0.6.</span> <span class="nav-text">注释</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#数组"><span class="nav-number">1.0.4.0.7.</span> <span class="nav-text">数组</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-组件"><span class="nav-number">1.0.5.</span> <span class="nav-text">React 组件</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#定义组件"><span class="nav-number">1.0.5.0.1.</span> <span class="nav-text">定义组件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#复合组件"><span class="nav-number">1.0.5.0.2.</span> <span class="nav-text">复合组件</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-State-状态"><span class="nav-number">1.0.6.</span> <span class="nav-text">React State(状态)</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#State状态的概念-组件渲染用户界面的状态"><span class="nav-number">1.0.6.0.1.</span> <span class="nav-text">State状态的概念 组件渲染用户界面的状态</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#将生命周期方法添加到类中-React生命周期）"><span class="nav-number">1.0.6.0.2.</span> <span class="nav-text">将生命周期方法添加到类中 (React生命周期）</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#数据自顶向下流动-不知道组件状态的"><span class="nav-number">1.0.6.0.3.</span> <span class="nav-text">数据自顶向下流动 不知道组件状态的</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-Props"><span class="nav-number">1.0.7.</span> <span class="nav-text">React Props</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#使用Props"><span class="nav-number">1.0.7.0.1.</span> <span class="nav-text">使用Props</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#默认Props"><span class="nav-number">1.0.7.0.2.</span> <span class="nav-text">默认Props</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#State和Props"><span class="nav-number">1.0.7.0.3.</span> <span class="nav-text">State和Props</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#Props验证（propTypes）"><span class="nav-number">1.0.7.0.4.</span> <span class="nav-text">Props验证（propTypes）</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-事件处理"><span class="nav-number">1.0.8.</span> <span class="nav-text">React 事件处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-条件渲染"><span class="nav-number">1.0.9.</span> <span class="nav-text">React 条件渲染</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-列表-amp-Keys"><span class="nav-number">1.0.10.</span> <span class="nav-text">React 列表 &amp; Keys</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-组件-API"><span class="nav-number">1.0.11.</span> <span class="nav-text">React 组件 API</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#React-API"><span class="nav-number">1.0.11.0.1.</span> <span class="nav-text">React API</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-组件生命周期"><span class="nav-number">1.0.12.</span> <span class="nav-text">React 组件生命周期</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-AJAX"><span class="nav-number">1.0.13.</span> <span class="nav-text">React AJAX</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-表单与事件"><span class="nav-number">1.0.14.</span> <span class="nav-text">React 表单与事件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#React-Refs"><span class="nav-number">1.0.15.</span> <span class="nav-text">React Refs</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="heweiliang"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">heweiliang</p>
  <div class="site-description" itemprop="description">heweiliang blog</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">96</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:yourname@gmail.com" title="E-Mail → mailto:yourname@gmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://plus.google.com/yourname" title="Google → https:&#x2F;&#x2F;plus.google.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-google"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://stackoverflow.com/yourname" title="StackOverflow → https:&#x2F;&#x2F;stackoverflow.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-stack-overflow"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title2</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yoursite.com/" title="http:&#x2F;&#x2F;yoursite.com">Title3</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">heweiliang</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">376k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:42</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"left","width":300,"height":600},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
